package com.baiyangtx.lingerweather;

import java.io.IOException;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.baiyangtx.lingerweather.adapt.CityKey;

import android.app.Activity;
import android.content.Intent;
import android.content.res.XmlResourceParser;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class CityChooseActivity extends Activity{

	private final static int TEXTSIZE = 25 ;
	
	
	private GridView hotCityView ;
	private GridView provinceView;
	
	private CityChooseDialog dialog ;
	
	
	/*
	 * list for hot city.
	 * pair< city name , city code >
	 */
	private ArrayList<Pair<String, String>> hotCity = new ArrayList<Pair<String,String>>() ;
	
	private ArrayList<String> provinceName  = new ArrayList<String>() ;
	
	
	

	private final OnItemClickListener hotCityItemListener = new  OnItemClickListener(){

		@Override
		public void onItemClick(
				AdapterView<?> parent, 
				View view, 
				int position,
				long id) {
			
			@SuppressWarnings("unchecked")
			Pair<String, String> city = (Pair<String, String>) parent.getItemAtPosition(position);
			
			Intent result = new Intent() ;
			result.putExtra( CityKey.CITY_CODE , city.second ) ;
			result.putExtra( CityKey.CITY_NAME , city.first ) ;
			setResult(RESULT_OK, result ) ;
			finish() ;
		}
		
	} ;
	
	private final OnItemClickListener provinceListener = new  OnItemClickListener(){

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			String province = (String) parent.getItemAtPosition(position) ;
			dialog = new CityChooseDialog( CityChooseActivity.this , province, citySelected) ;
			dialog.show() ;
		}
		
	} ;
	
	private final CitySelectedListener citySelected = new CitySelectedListener() {
		
		@Override
		public void onCitySelected(String name, String code) {
			dialog.dismiss() ;
			Intent result = new Intent() ;
			result.putExtra(CityKey.CITY_CODE, code) ;
			result.putExtra( CityKey.CITY_NAME , name	) ;
			setResult(RESULT_OK, result) ;
			finish() ;
		}
	};
	
	
	
	
	private class GridViewAdapt extends BaseAdapter {

		private boolean hotCityGridView = false ;

		private static final int hotCityBaseId = 0x0001 ;
		private static final int provinceBaseId = 0x1000 ;
		
		public GridViewAdapt(boolean hotCity) {
			hotCityGridView = hotCity ;
		}
		
		@Override
		public int getCount() {
			int size = 0 ;
			if(hotCityGridView){
				size = hotCity.size() ;
			}else{
				size = provinceName.size() ;
			}
			return size ;
		}

		@Override
		public Object getItem(int position) {
			Object item = null ;
			if( hotCityGridView ){
				item = hotCity.get(position) ;
			}else{
				item = provinceName.get(position) ;
			}
			return item ;
		}

		@Override
		public long getItemId(int position) {
			int id = 0 ;
			if( hotCityGridView ){
				id = hotCityBaseId + position ;
			}else{
				id = provinceBaseId + position ;
			}
			return id ;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			TextView textView = null ;
			
			if( convertView != null ){
				return convertView ;
			}else{
				textView = new TextView(CityChooseActivity.this) ;
				String text = null ;
				if( hotCityGridView ){
					text = hotCity.get(position).first ;
				}else{
					text = provinceName.get(position) ;
				}
				textView.setText(text) ;
				textView.setTextColor(Color.WHITE) ;
				textView.setTextSize(TEXTSIZE);
			}
			return textView ;
		}
		
	}
	

	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.requestWindowFeature( Window.FEATURE_NO_TITLE ) ;
		setContentView(R.layout.city_choose) ;
		try {
			parserCityXMlFile() ;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		hotCityView = (GridView)findViewById(R.id.gridview_hotcity) ;
		provinceView = (GridView)findViewById( R.id.gridview_province ) ;
		
		hotCityView.setAdapter(new GridViewAdapt(true) );
		provinceView.setAdapter(new GridViewAdapt(false) ) ;
		Drawable selector = new ColorDrawable(Color.TRANSPARENT) ;
		hotCityView.setSelector(selector) ;
		provinceView.setSelector(selector) ;
		
		hotCityView.setOnItemClickListener(hotCityItemListener );
		provinceView.setOnItemClickListener( provinceListener ) ;
	}
	
	
	
	
	
	
	
	private void parserCityXMlFile() throws XmlPullParserException, IOException{
		final String HOT_CTIY = "hotcity" ;
		final String PROVINCE = "province" ;
		final String CITY = "city" ;
		
		XmlResourceParser parser = getResources().getXml(R.xml.city_code) ;
		
		int event = parser.getEventType() ;
		boolean isParserHotCity = false ;
		while( event != XmlResourceParser.END_DOCUMENT ){
			String name = parser.getName() ;
			
			switch(event){
			case XmlResourceParser.START_TAG :
				if( HOT_CTIY.equals(name) ) {
					isParserHotCity = true ;
				}else if( CITY.equals(name) && isParserHotCity ){
					String city = parser.getAttributeValue(0) ;
					String code = parser.nextText() ;
					Pair<String, String> pair = new Pair<String, String>(city, code) ;
					hotCity.add(pair) ;
				}else if( PROVINCE.equals(name) ){
					String province = parser.getAttributeValue(0) ;
					provinceName.add(province) ;
				}
				break ;
			case XmlPullParser.END_TAG :
				if( HOT_CTIY.equals(name) ){
					isParserHotCity = false ;
				}
				break ;
			}
			
			event = parser.next() ;
		}
		parser.close() ;
	}
	
	
	
	
	
	
	
	
}
